/**

 * User: seeker910
 * Date: 13-10-24
 * Time: 下午10:18
  @description 具有tab标签的 PageContainer  
 */ 
Rsd.define('Rsd.container.TabPage', {
    extend: 'Rsd.container.PageContainer',
    requires:[
        'Rsd.control.Svg',
        'Rsd.control.TabBar'
    ],
    xtype: 'tab-page',
    width: '100%',
    closeBtn:true,
    bodyTagName:'div',
    bodyCls: 'x-tab-body',
    positionX: 20,
    offsetX: 12.0,
    currentTab: null,
    selectBg: null,
    fillColor: "#EEEEEE",
    selectedColor: "#1B9DD4",
    /*
    * svg collection
    * */
    tabs:{},
    layout:'hbox',
    header:{
        xtype:'tab-bar',
        cls: 'x-tab-header',
        header:false,
        visible: true,
        position: 'top',
        height:42,
        style:{fontSize:'100%'},
        space: 0
    },
    /*
    * */
    constructor: function TabPageContainer(config) {

        config = config || {};
        this.apply(config);
        
    },
    /**
     * */
    onAfterRender:function onAfterRender() {
        this.callParent();
        var me = this;
        this.header.content.closeBtn = this.closeBtn;
        this.header.content.on('afterremoved',function(tab){
            me.remove(tab.key);
        });
        //由 tab 选择 后 驱动页面选择
        this.header.content.on('afterselected',function(tab){
            me.callParentFn("select",[tab.key]); 
        });
    },
    /*
    * */
    doLayout: function doLayout() {
        var me = this;
        me.callParent();
        //
        if (me.currentPage) {
            setTimeout(function(){
                me.select(me.currentPage.id); 
            },100);
            
        }
    },
    
    /**
     *
     * */
    select: function select(key,speed) {

        this.header.content.select(key);

        this.callParent(key,speed);
         
        //this.events.fire(this, 'afterselected',[{tab: this.currentTab,page:this.getCurrentPage()}] );
    },
    /*
     * */
    remove: function remove(id) {
 
        this.callParent(id);
        //
        //this.header.content.remove(id);
        //this.events.fire(this, 'afterremoved', [{tab: this.tabs[id],page:this.getCurrentPage()}]);
    },
    /*
     page.selected控制tab selected样式
     speed:动画显示速度
     * */
    add: function add(page,speed) {
        var me = this;
        

        if (me.muliti) {

        }
        else {
            //remove all tabs
            me.tabs={}; 
        }

        if (!me.contain(page.id)) {

            //add tab
            var _tab = me.header.content.add(page.id, page.tabTitle||'tabTitle未设置');
             
            //添加page
            me.callParent(page,speed); 
            
        } 
        //选择
        if(page.selected){
            me.select(page.id,speed); 
        }
    },
    /*
    * */
    getCurrentPage: function getCurrentPage() {
        return this.currentPage;
    } 
});
